﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SPA_Prototype.Model;

namespace SPA_Prototype
{
    public partial class FormBillDetails : Form
    {       
        public string BillNumber;  
 
        private History _History;
        //constructor
        public FormBillDetails(History ht)
        {
            _History = ht;
            InitializeComponent();                      
        }

        private void Bill_Details_Load(object sender, EventArgs e)
        {                        
            if (BillNumber != null)
            {
                billMainTableBindingSource.DataSource = _History.Search(SEARCH_METHOD.BILL_NO, TIME_CONSTRAIN.ALL, BillNumber);
                billDetailsTableBindingSource.DataSource = _History.BillDetails(BillNumber);
            }
        }

        private void BillSearch()
        {
            BillNumber = CbBillNo.Text;
            billMainTableBindingSource.DataSource = _History.Search(SEARCH_METHOD.BILL_NO, TIME_CONSTRAIN.ALL, BillNumber);
            billDetailsTableBindingSource.DataSource = _History.BillDetails(BillNumber);
        }

        private void CbBillNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (sender == CbBillNo)
            {
                if (e.KeyValue == 13)
                    BillSearch();
            }                        
        }

        private void CbBillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            BillSearch();
        }

        private void Menu_Click(object sender, EventArgs e)
        {
            if (sender == Menu_Close)            Close();           
            if (sender == Menu_Export2Excel)
            {
                string path = Path.Combine(Application.StartupPath, "Excel Templates\\Blank.xls");
                Export2Excel exp2Excel = new Export2Excel();
                exp2Excel.ExportFromDatgridView(dtgHistoryDetails, path, "Details");
            }
        }
    }
}
